//
//  GameEngine.m
//  FlappyBird
//
//  Created by huangdl on 14-9-23.
//  Copyright (c) 2014年 1000phone. All rights reserved.
//

#import "GameEngine.h"

@implementation GameAction

@end


@implementation GameEngine
{
    NSTimer *_timer;
    NSMutableArray *_actionArray;
}
+(id)sharedEngine
{
    static GameEngine *_e = nil;
    if (!_e) {
        _e = [[GameEngine alloc]init];
    }
    return _e;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        _actionArray = [[NSMutableArray alloc]init];
    }
    return self;
}

static int count = 0;
-(void)timerAction
{
    count ++;
    for (GameAction *a in _actionArray) {
        if (a.isValid && count%a.timer == 0) {
            a.callback();
        }
    }
}

-(void)registerAction:(void(^)())block andTimer:(int)timer andName:(NSString *)name
{
    GameAction *action = [[GameAction alloc]init];
    action.callback = block;
    action.timer = timer;
    action.name = name;
    action.isValid = YES;
    [_actionArray addObject:action];
}

-(void)setValid:(BOOL)isvalid ForName:(NSString *)name
{
    for (GameAction *a in _actionArray) {
        if ([a.name isEqualToString:name]) {
            a.isValid = isvalid;
        }
    }
}

-(void)gameOver
{
    [_timer setFireDate:[NSDate distantFuture]]; 
}

-(void)restart
{
    [_timer setFireDate:[NSDate date]];
}

@end








